class LinkNode {
  constructor(val) {
    this.value = val;
    this.next = null;
  }
}

class LinkList {
  constructor() {
    const node = new LinkNode(null);
    this.linkSet = [node];
    this.head = node;
    this.tail = node;
  }
  push(val) {
    let node = val instanceof LinkNode ? val : new LinkNode(val);
    this.tail.next = node;
    this.tail = node;
    this.linkSet.push(node);
  }
  unshift(val) {
    let node = val instanceof LinkNode ? val : new LinkNode(val);
    node.next = this.head;
    this.head = node;
    this.linkSet.push(node);
  }
  display() {
    const res = [];
    let cur = this.head;
    while (cur.next) {
      cur = cur.next;
      res.push(cur.value);
    }
    console.log(res);
  }
  reverse() {
    let tmp = this.head;
    let pre = tmp.next;
    let next = pre.next;

    pre.next = null;
    while (next) {
      tmp = next.next;
      next.next = pre;
      pre = next;
      next = tmp;
    }

    this.tail = this.head.next;
    this.head.next = pre;
  }
}

const list = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const link = new LinkList();

list.forEach((item) => {
  link.push(item);
});

link.display();
link.reverse();
link.display();
console.log(link)
